package com.ymatou.iapi.hunter.testcase;

import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.hunter.paramater.GetHunterContributionListRequest;
import com.ymatou.iapi.hunter.service.GetHunterContributionListCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.hunter.HunterRelation;
import com.ymttest.database.sqlwapper.hunter.HunterRelationWrapper;
import com.ymttest.utils.DataUtil;

public class Ts_GetHunterContributionList {
	
	private static GetHunterContributionListRequest getHunterContributionListRequest;
	private static GetHunterContributionListCall getHunterContributionListCall;
	HunterRelationWrapper wrapper = new HunterRelationWrapper();

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("查看推手贡献团队");
	}

	@Before
	public void caseUp() {
		getHunterContributionListRequest = new GetHunterContributionListRequest();
		getHunterContributionListCall = new GetHunterContributionListCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getHunterContributionListCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	public void verifyDb(long hunterId, JSONObject object) throws JSONException {
		
		List<HunterRelation> actualResult = wrapper.selectHunterContributionList(hunterId);
		int contributionTotal = 0;
		
		for(int i = 0; i < actualResult.size(); i++) {

			long userId = object.getJSONArray("userList")
					.getJSONObject(i).getLong("userId");
			
			Logger.verifyEquals(userId, actualResult.get(i).getHunter_id(),
					"验证userId");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getString("userName"), actualResult.get(i).getHunter_name(),
					"验证userName");
			DataUtil.verifyDateEquals(object.getJSONArray("userList")
					.getJSONObject(i).getString("addTime"), actualResult.get(i).getAdd_time(),
					"验证addTime");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getInt("hunterLevel"), actualResult.get(i).getHunter_level(),
					"验证hunterLevel");
			Logger.verifyEquals(object.getJSONArray("userList")
					.getJSONObject(i).getInt("childrenTotal"), actualResult.get(i).getChildren_total(),
					"验证childrenTotal");
			
			contributionTotal += object.getJSONArray("userList")
					.getJSONObject(i).getInt("childrenTotal");
		}
		
		Logger.verifyEquals(object.getInt("totalRecords"), actualResult.size(),
				"验证totalRecords");
		Logger.verifyEquals(object.getInt("contributionTotal"), contributionTotal + object.getInt("totalRecords"),
				"验证contributionTotal");
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterContributionList_001() {
		Logger.start(true, "查看推手贡献团队");
		try {
			
			long hunterId = 4321L;
			
			getHunterContributionListRequest.setHunterId(hunterId);
			getHunterContributionListRequest.setPageIndex(1);
			getHunterContributionListRequest.setPageSize(100);
			
			getHunterContributionListCall.setData(getHunterContributionListRequest);
			getHunterContributionListCall.callService();
			
			Logger.verifyEquals("success", getHunterContributionListCall.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(0, getHunterContributionListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(true, getHunterContributionListCall.getResponseData().getBoolean("success"),
					"验证success");
			
			verifyDb(hunterId, getHunterContributionListCall.getResult());
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterContributionList_002() {
		Logger.start(true, "hunter为0");
		try {
			
			long hunterId = 0;
			
			getHunterContributionListRequest.setHunterId(hunterId);
			getHunterContributionListRequest.setPageIndex(1);
			getHunterContributionListRequest.setPageSize(10);
			
			getHunterContributionListCall.setData(getHunterContributionListRequest);
			getHunterContributionListCall.callService();
			
			Logger.verifyEquals("hunterId:推手用户编号不能为空", getHunterContributionListCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterContributionListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterContributionListCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterContributionList_003() {
		Logger.start(true, "pageSize为0");
		try {
			
			long hunterId = 4321;
			
			getHunterContributionListRequest.setHunterId(hunterId);
			getHunterContributionListRequest.setPageIndex(1);
			getHunterContributionListRequest.setPageSize(0);
			
			getHunterContributionListCall.setData(getHunterContributionListRequest);
			getHunterContributionListCall.callService();
			
			Logger.verifyEquals("pageSize:每页显示的条数不能为空", getHunterContributionListCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterContributionListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterContributionListCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category(P1.class)
	@TestCase
	public void Tc_GetHunterContributionList_004() {
		Logger.start(true, "pageIndex为0");
		try {
			
			long hunterId = 4321;
			
			getHunterContributionListRequest.setHunterId(hunterId);
			getHunterContributionListRequest.setPageIndex(0);
			getHunterContributionListRequest.setPageSize(10);
			
			getHunterContributionListCall.setData(getHunterContributionListRequest);
			getHunterContributionListCall.callService();
			
			Logger.verifyEquals("pageIndex:当前页码不能为空", getHunterContributionListCall
					.getResponseData().getString("resultMessage"),
					"验证resultMessage");
			Logger.verifyEquals(2, getHunterContributionListCall.getResponseData().getInt("resultCode"),
					"验证resultCode");
			Logger.verifyEquals(false, getHunterContributionListCall.getResponseData().getBoolean("success"),
					"验证success");
			
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
